package com.lw.lost.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.lw.lost.R
import com.lw.lost.databinding.ItemManageUserBinding
import com.lw.lost.models.ManageUser

class ManageUserAdapter(
    private val onUserAction: (ManageUser) -> Unit,
    private val onUserClick: (ManageUser) -> Unit
) : ListAdapter<ManageUser, ManageUserAdapter.ViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemManageUserBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position), onUserAction, onUserClick)
    }

    class ViewHolder(
        private val binding: ItemManageUserBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(user: ManageUser, onUserAction: (ManageUser) -> Unit, onUserClick: (ManageUser) -> Unit) {
            binding.apply {
                // 显示用户名和姓名
                tvName.text = "${user.name} (${user.username})"
                
                // 显示用户基本信息
                tvInfo.text = "${if (user.gender == 0) "男" else "女"} · ${user.age}岁 · ${user.phone}"
                
                // 设置状态文本和颜色
                tvStatus.text = when (user.status) {
                    0 -> "已封禁"
                    1 -> "正常"
                    else -> "未知"
                }
                tvStatus.setTextColor(
                    root.context.getColor(
                        when (user.status) {
                            0 -> android.R.color.holo_red_dark
                            1 -> android.R.color.holo_green_dark
                            else -> android.R.color.darker_gray
                        }
                    )
                )

                // 设置操作按钮
                btnAction.text = when (user.status) {
                    0 -> "解封"
                    1 -> "封禁"
                    else -> "无操作"
                }
                btnAction.isEnabled = user.status < 2
                
                if (user.status < 2) {
                    btnAction.setOnClickListener { onUserAction(user) }
                }

                // 加载头像
                Glide.with(ivAvatar)
                    .load(user.avatar)
                    .placeholder(R.drawable.ic_profile)
                    .error(R.drawable.ic_profile)
                    .into(ivAvatar)

                // 添加点击事件
                root.setOnClickListener { onUserClick(user) }
            }
        }
    }

    private class DiffCallback : DiffUtil.ItemCallback<ManageUser>() {
        override fun areItemsTheSame(oldItem: ManageUser, newItem: ManageUser) =
            oldItem.userId == newItem.userId

        override fun areContentsTheSame(oldItem: ManageUser, newItem: ManageUser) =
            oldItem == newItem
    }
} 